﻿using System;
using System.ComponentModel.Design;
using Microsoft.VisualStudio.Shell;
using PS_WSSDeploy_Package.Application;
using PS_WSSDeploy_Package.Utils;

namespace PS_WSSDeploy_Package.WdCommand
{
    class RecycleAppPoolsCmd : BaseCmd, IWdTask
    {
        public static BaseCmd Create(OleMenuCommandService service, bool enableForSandbox)
        {
            var instance = new RecycleAppPoolsCmd();
            var cmdId = new CommandID(GuidList.guidPS_WSSDeploy_PackageCmdSet, (int)PkgCmdIDList.cmdidRecycleAppPoolsCommand);
            AddCommandToMenu(service, instance, cmdId, enableForSandbox);
            return instance;
        }

        public override void DoExecute()
        {
            AddTask(this);
        }

        public void TaskExecute()
        {
            try
            {
                Cc.RecycleAppPools();
                WriteOutput(WssDeployResources.RecycleAppPoolsCmd_RecycleSucceeded);
                SetSuccess();
            }
            catch (Exception ex)
            {
                WriteOutput(string.Format(WssDeployResources.RecycleAppPoolsCmd_RecycleError, ex.Message));
                SetFailed();
            }
        }
    }
}
